package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.tanhua.common.enums.SexEnum;
import com.tanhua.common.pojo.Question;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.api.HuanXinApi;
import com.tanhua.dubbo.server.api.UserLikeApi;
import com.tanhua.dubbo.server.api.UserLocationApi;
import com.tanhua.dubbo.server.api.VisitorsApi;
import com.tanhua.dubbo.server.enums.HuanXinMessageType;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.dubbo.server.vo.UserLocationVo;
import com.tanhua.server.vo.NearUserVo;
import com.tanhua.server.vo.TodayBest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class TanHuaService {

    @Autowired
    private RecommendUserService recommendUserService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private QuestionService questionService;

    @Reference(version = "1.0.0")
    private HuanXinApi huanXinApi;

    @Reference(version = "1.0.0")
    private VisitorsApi visitorsApi;

    @Reference(version = "1.0.0")
    private UserLocationApi userLocationApi;

    @Reference(version = "1.0.0")
    private UserLikeApi userLikeApi;

    @Value("${tanhua.default.recommend.users}")
    private String defaultRecommendUsers;

    @Autowired
    private IMService imService;

    public TodayBest queryUserInfo(Long userId) {
        UserInfo userInfo = this.userInfoService.queryUserInfoByUserId(userId);
        if(ObjectUtil.isEmpty(userInfo)){
            return null;
        }

        TodayBest todayBest = new TodayBest();
        todayBest.setId(userId);
        todayBest.setAge(userInfo.getAge());
        todayBest.setGender(userInfo.getSex().name().toLowerCase());
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setTags(Convert.toStrArray(
                StrUtil.split(userInfo.getTags(),",")));
        todayBest.setAvatar(userInfo.getLogo());

        //缘分值
        User user = UserThreadLocal.get();
        todayBest.setFateValue(this.recommendUserService.queryScore(userId,user.getId()).longValue());

        //记录来访用户
        this.visitorsApi.saveVisitor(userId,user.getId(),"个人主页");

        return todayBest;
    }

    public String queryQuestion(Long userId) {
        Question question = this.questionService.queryQuestion(userId);
        if(ObjectUtil.isNotEmpty(question)){
            return question.getTxt();
        }
        //返回一个系统设置的问题
        return "你的爱好是什么?";
    }

    public Boolean replyQuestion(Long userId, String reply) {
        User user = UserThreadLocal.get();
        UserInfo userInfo = this.userInfoService.queryUserInfoByUserId(user.getId());

        //构建消息内容
        Map<String,Object> msg = new HashMap<>();
        msg.put("userId",user.getId());
        msg.put("huanxinId",user.getId());
        msg.put("nickname",userInfo.getNickName());
        msg.put("strangerQuestion",this.queryQuestion(userId));
        msg.put("reply",reply);

        //发送环信消息
        return this.huanXinApi.sendMsgFromAdmin("HX_"+userId,
                HuanXinMessageType.TXT, JSONUtil.toJsonStr(msg));

    }

    public List<NearUserVo> queryNearUser(String gender, String distance) {

        User user = UserThreadLocal.get();
        UserLocationVo userLocationVo = this.userLocationApi.queryByUserId(user.getId());
        if(ObjectUtil.isEmpty(userLocationVo)){
            return ListUtil.empty();
        }

        PageInfo<UserLocationVo> pageInfo = this.userLocationApi.queryUserFromLocation(userLocationVo.getLongitude()
                , userLocationVo.getLatitude()
                , Convert.toDouble(distance)
                , 1, 50);
        List<UserLocationVo> records = pageInfo.getRecords();
        if(CollUtil.isEmpty(records)){
            return ListUtil.empty();
        }

        //构造筛选条件
        List<Object> userIdList = CollUtil.getFieldValues(records, "userId");
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id",userIdList);
        if(StrUtil.equalsIgnoreCase(gender,"man")){
            queryWrapper.eq("sex", SexEnum.MAN);
        }else if (StrUtil.equalsIgnoreCase(gender,"woman")){
            queryWrapper.eq("sex", SexEnum.WOMAN);
        }

        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoList(queryWrapper);
        List<NearUserVo> result = new ArrayList<>();
        for (UserLocationVo record : records) {
            if(ObjectUtil.equals(record.getUserId(),user.getId())){
                continue;
            }
            for (UserInfo userInfo : userInfoList) {
                if(ObjectUtil.equals(record.getUserId(),userInfo.getId())){
                    NearUserVo nearUserVo = new NearUserVo();
                    nearUserVo.setUserId(record.getUserId());
                    nearUserVo.setAvatar(userInfo.getLogo());
                    nearUserVo.setNickname(userInfo.getNickName());

                    result.add(nearUserVo);
                    break;
                }
            }
        }
        return result;

    }

    public List<TodayBest> queryCardsList() {
        User user = UserThreadLocal.get();
        int count = 50;

      List<RecommendUser> recommendUserList =  this.recommendUserService.queryCardList(user.getId(),count);
      if(CollUtil.isEmpty(recommendUserList)){
          recommendUserList = new ArrayList<>();

          String[] list = StrUtil.split(defaultRecommendUsers, ",");
          for (String userId: list) {
              RecommendUser recommendUser = new RecommendUser();
              recommendUser.setUserId(Long.valueOf(userId));
              recommendUser.setToUserId(user.getId());
              recommendUserList.add(recommendUser);
          }
      }
      int showNum = Math.min(10,recommendUserList.size());
      List<RecommendUser> result = new ArrayList<>();
        for (int i = 0; i < showNum; i++) {
            //todo 可能重复
            int index = RandomUtil.randomInt(0, recommendUserList.size());
            RecommendUser recommendUser = recommendUserList.get(index);
            result.add(recommendUser);
        }

        List<Object> userIdList = CollUtil.getFieldValues(result, "userId");
        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoList(userIdList);
        List<TodayBest> todayBests = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            TodayBest todayBest = new TodayBest();
            todayBest.setId(userInfo.getUserId());
            todayBest.setAge(userInfo.getAge());
            todayBest.setAvatar(userInfo.getLogo());
            todayBest.setGender(userInfo.getSex().name().toLowerCase());
            todayBest.setNickname(userInfo.getNickName());
            todayBest.setTags(Convert.toStrArray(StrUtil.split(userInfo.getTags(), ',')));
            todayBest.setFateValue(0L);

            todayBests.add(todayBest);
        }
        return todayBests;
    }

    public void likeUser(Long likeUserId) {
        User user = UserThreadLocal.get();
        Boolean result = this.userLikeApi.likeUser(user.getId(), likeUserId);
        if(!result){
            log.error("添加喜欢失败~ user="+user.getId());
            return;
        }
        //判断双方是否互相喜欢 决定是否添加为好友
        if(this.userLikeApi.isMutualLike(user.getId(),likeUserId)){
            this.imService.contactUser(likeUserId);
        }

    }

    public void notLikeUser(Long likeUserId) {
        User user = UserThreadLocal.get();
         this.userLikeApi.notLikeUser(user.getId(), likeUserId);
    }
}
